package com.phpxiu.app.download;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @file
 * @brief 自定义封装okhttp下载图片回调
 * @author 吴金洲
 * @note okhttp下载图片时设置的监听回调
 *
 */
public class PicDownloadCallBack extends Handler implements Callback {
    public final static String PIC_CACHE= Environment.getExternalStoragePublicDirectory("/").getPath()+"/com.phpxiu.app.kky/image/";
    private final static int DOWNLOAD_SUCCESS=-123456;
    private String id;//唯一标识 url的MD5码形式
    private File mFile;//缓存文件
    public PicDownloadCallBack(String id) {
        this.id = id;
        File file=new File(PIC_CACHE);
        if(!file.exists()){
            file.mkdirs();
        }
        mFile=new File(PIC_CACHE,id+".jpg");
    }

    /**
     * 请求下载失败
     * @param call
     * @param e
     */
    @Override
    public void onFailure(Call call, IOException e) {
        onErr();
    }

    /**
     * 请求下载成功
     * @param call
     * @param response
     * @throws java.io.IOException
     */
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.i("","文件路径："+mFile.getPath());
        InputStream is = null;
        FileOutputStream fos=null;
        BufferedOutputStream bos=null;
        try {
            is = response.body().byteStream();//获取响应字节流
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            fos=new FileOutputStream(mFile);
            bos = new BufferedOutputStream(fos);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            Message msg=obtainMessage();
            msg.what=DOWNLOAD_SUCCESS;
            msg.obj=Uri.fromFile(mFile);
            sendMessage(msg);
        } catch (Exception e) {
            onErr();
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {

            }
            try {
                if(bos!=null){
                    bos.close();
                }
            }catch (IOException e){

            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
            }
        }
    }



    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case DOWNLOAD_SUCCESS:
                onDownloadSuccess((Uri)msg.obj);
                break;
            default:
                break;
        }
    }

    /**
     * 出错异常,停止
     */
    public void onErr(){

    }

    public void onDownloadSuccess(Uri uri){

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
